/**
	Copyright (c) 2010 ATTi 
	All rights reserved.
	Author: Michael Shu
	Date: 2/26/2010
*/

trigger ClockCounter on Clock__c (after insert, after update) {
	Boolean isFirst = true;
	
	if(isFirst){
		isFirst = false;
		System.debug('Test ClockCounter on Clock__c exetued ');	
		List<Lead> leads = new List<Lead>();
		Set<ID> leadIds = new Set<ID>();
		
		for(Clock__c ck : trigger.new){
			if(ck.IsTimeOut__c){
				leadIds.add(ck.Lead__c);
			}		
		}
		
		System.debug('Test leadIds.isEmpty() =  ' + leadIds.isEmpty());	
		if(!leadIds.isEmpty()){
			List<Lead> extLeads = [select Id, isConverted  from Lead where ID IN: leadIds];
			for(Lead l : extLeads){
				if(!l.isConverted){
					l.IsOver7Days__c = true;
					leads.add(l);
				}
			}
		}
		
		if(!leads.isEmpty()){
			update leads;
		}
	}
}